package com.eascs.workflow.processAuthorization.service.impl;


import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.processAuthorization.dao.impl.ProcessAuthorizationDao;
import com.eascs.workflow.processAuthorization.entity.ProcessAuth;
import com.eascs.workflow.processAuthorization.service.IProcessAuthorizationService;
import com.eascs.workflow.processAuthorization.vo.ProcessAuthorizationVo;


@Service
public class ProcessAuthorizationService implements IProcessAuthorizationService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Inject
	ProcessAuthorizationDao processAuthorizationDao;
	
	@Override
	public PageResult<Object[]> findProcessAuthorizationInfo(ProcessAuthorizationVo vo) {
		return processAuthorizationDao.findProcessAuthorizationInfo(vo);
	}
	
	@Override
	public ModelResult<Boolean> saveProcessAuth(ProcessAuthorizationVo vo){
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		boolean  flag = isAuthExists(vo);
		if(flag){//判断是否已经添加该授权,大于0表示已经授权
			model.setErrCode("exist");
			logger.info("saveProcessAuth.exist.ProcessAuthorizationService");
		}else{
			ProcessAuth pa = setPorperty(vo);
			//未添加，则添加新的授权
			ProcessAuth p =processAuthorizationDao.insert(pa);
			if(p!=null){
				model.withModel(true);
			}else{
				model.setErrCode("error");
				logger.info("saveProcessAuth.error.ProcessAuthorizationService,exception:{}");
				throw new RuntimeException("数据库异常,添加流程授权失败");
			}
		}
		return model;
	}

	@Override
	public ModelResult<Boolean> updateProcessAuthStatus(ProcessAuthorizationVo vo){
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		ProcessAuth pa = processAuthorizationDao.findProcessAuthById(vo.getId());
		vo.setProcessMainCode(pa.getProcessMainCode());
		vo.setProcessLessCode(pa.getProcessLessCode());
		vo.setAuthBeginDate(pa.getAuthBeginDate());
		vo.setAuthEndDate(pa.getAuthEndDate());
		vo.setRollOuterCode(pa.getRollOuterCode());
		if(("N").equals(pa.getAuthStatus())){
			boolean  flag = isAuthExists(vo);
			if(flag){//判断是否已经添加该授权,大于0表示已经授权
				model.setErrCode("exist");
				logger.info("saveProcessAuth.exist.ProcessAuthorizationService");
			}else{
				//未添加，则添加新的授权
				int i  =processAuthorizationDao.updateProcessAuthStatus(vo.getId(), "Y");
				if(i>0){
					model.withModel(true);
				}else{
					model.setErrCode("error");
					logger.info("saveProcessAuth.error.ProcessAuthorizationService,exception:{}");
					throw new RuntimeException("数据库异常,添加流程授权失败");
				}
			}
		}else{
			int i= processAuthorizationDao.updateProcessAuthStatus(vo.getId(), "N");
			if(i>0){
				model.withModel(true);
			}else{
				model.setErrCode("error");
				logger.info("saveProcessAuth.error.ProcessAuthorizationService,exception:{}");
				throw new RuntimeException("数据库异常,添加流程授权失败");
			}
		}
		
		return model;
	}
	
	@Override
	public User  findProcessAuthPerson(String psnCode,String processType){
		return processAuthorizationDao.findProcessAuthPerson(psnCode, processType);
	}
	
	private boolean  isAuthExists(ProcessAuthorizationVo vo) {
		boolean  flag =false;
		String sql ="select count(0) from process_auth where auth_status = 'Y' and auth_bigin_date is not null  and process_main_code = ? and process_less_code = ? and roll_outer_code = ?   and auth_bigin_date < ? and  auth_end_date > ? ";
		Object[] param ={vo.getProcessMainCode(),vo.getProcessLessCode(),vo.getRollOuterCode(),vo.getAuthEndDate(),vo.getAuthBeginDate()}; 
		List<Object[]> list = processAuthorizationDao.findObjectsList(sql, param);
		if(list!=null && list.size()>0){
			Object[] o = list.get(0);
			Long i =(Long) o[0];
			if(i>0){
				flag=true;
			}
		}  
		  return flag;
	}

	private ProcessAuth setPorperty(ProcessAuthorizationVo vo) {
		ProcessAuth pa=new ProcessAuth();
		pa.setProcessMain(vo.getProcessMain());
		pa.setProcessMainCode(vo.getProcessMainCode());
		pa.setProcessLess(vo.getProcessLess());
		pa.setProcessLessCode(vo.getProcessLessCode());
		pa.setRollOuterName(vo.getRollOuterName());
		pa.setRollOuterCode(vo.getRollOuterCode());
		pa.setRollInerName(vo.getRollInerName());
		pa.setRollInerCode(vo.getRollInerCode());
		pa.setAuthBeginDate(vo.getAuthBeginDate());
		pa.setAuthEndDate(vo.getAuthEndDate());
		pa.setAuthStatus(vo.getAuthStatus());
		pa.setCreateDate(new Date());
		pa.setCreator(UserHolder.getUser().getCname());
		return pa;
	}

}
